
package gui.panels;

import gui.MainFrame;
import gui.listeners.GuiActionEvent;
import gui.listeners.GuiActionEventSource;
import gui.listeners.GuiActionEventType;
import gui.listeners.GuiActionListener;
import gui.panels.components.MmiButton;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author JtR
 */
public abstract class GuiPanel extends JPanel implements GuiActionEventSource {
    private List<GuiActionListener> listeners;
    private MmiButton statisticsButton;
    protected MainFrame parent;
    protected GridBagConstraints constraints = new GridBagConstraints();
    protected static Image teaBackground;
    protected static Image coffeeBackground;
    protected static Image bothBackground;
    protected static Image profileBackground;
    protected BackgroundType backgroundType;
    
    public GuiPanel(MainFrame parent, BackgroundType backgroundType) {
        this(parent, true, backgroundType);
    }
    
    public enum BackgroundType {
        TEA,
        COFFEE,
        BOTH,
        PROFILE
    }
    
    public GuiPanel(MainFrame parent, boolean addStatisticsButton, BackgroundType backgroundType) {
        this.parent = parent;
        this.backgroundType = backgroundType;
        if(teaBackground == null) {
            try {
                teaBackground = ImageIO.read(this.getClass().getClassLoader().getResource("res/tee_back.jpg")).getScaledInstance(parent.getWidth(), parent.getHeight(), Image.SCALE_SMOOTH);
                coffeeBackground = ImageIO.read(this.getClass().getClassLoader().getResource("res/kaffee_back.jpg")).getScaledInstance(parent.getWidth(), parent.getHeight(), Image.SCALE_SMOOTH);
                bothBackground = ImageIO.read(this.getClass().getClassLoader().getResource("res/both.jpg")).getScaledInstance(parent.getWidth(), parent.getHeight(), Image.SCALE_SMOOTH);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        
        listeners = new ArrayList<>();
        setLayout(new GridBagLayout());
        
        constraints.anchor = GridBagConstraints.NORTHWEST;
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = 1;
        constraints.weighty = 1;
        
        if(addStatisticsButton) {
            statisticsButton = new MmiButton("res/profil.png", 150);
            statisticsButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.OPEN_STATISTICS));
                }
            });
            add(statisticsButton, constraints);
        }
        initComponents();
    }
    

    @Override
    public void addGuiActionEventListener(GuiActionListener listener) {
        listeners.add(listener);
    }

    @Override
    public void fireGuiActionEvent(GuiActionEvent event) {
        for(GuiActionListener l : listeners) {
            l.GuiActionEventFired(event);
        }
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        switch(backgroundType) {
                case TEA:
                    g.drawImage(teaBackground, 0, 0, this);
                    break;
                case COFFEE:
                    g.drawImage(coffeeBackground, 0, 0, this);
                    break;
                case BOTH:
                    g.drawImage(bothBackground, 0, 0, this);
                    break;
                case PROFILE:
                    g.drawImage(coffeeBackground, 0, 0, this);
                    break;
            }
        
    }
    
    public abstract String getDescription();
    public abstract void updateFields();
    public abstract boolean getPreviousButtonEnabled();
    public abstract boolean getNextButtonEnabled();
    public abstract String getName();
    public abstract GuiPanel getNextPanel();
    public abstract GuiPanel getPreviousPanel();
    protected void initComponents(){}
}
